package com.ai.service.made.impl;

import com.ai.common.CommonResponse;
import com.ai.common.constant.RedisConstants;
import com.ai.entity.cameraFunctions.functions.CameraFunctionsPO;
import com.ai.entity.function.FunctionPO;
import com.ai.entity.made.OpcNodeCameraFunctionRelation;
import com.ai.entity.made.OpcNodeInfo;
import com.ai.mapper.made.OpcNodeCameraFunctionRelationMapper;
import com.ai.model.dto.OpcNodeCameraFunctionRelationDto;
import com.ai.service.made.CameraFunctionsService;
import com.ai.service.made.FunctionService;
import com.ai.service.made.IOpcNodeCameraFunctionRelationService;
import com.ai.service.made.IOpcNodeInfoService;
import com.ai.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2024-11-15
 */
@Service
@Slf4j
public class OpcNodeCameraFunctionRelationServiceImpl extends ServiceImpl<OpcNodeCameraFunctionRelationMapper, OpcNodeCameraFunctionRelation> implements IOpcNodeCameraFunctionRelationService {


    @Resource
    private IOpcNodeInfoService iOpcNodeInfoService;

    @Resource
    private CameraFunctionsService cameraFunctionsService;

    @Resource
    private FunctionService functionService;



    @Override
    public CommonResponse<String> add(OpcNodeCameraFunctionRelationDto opcNodeCameraFunctionRelationDto) {

        List<OpcNodeCameraFunctionRelation> dbNodeCameraFunctionRelations = list(new QueryWrapper<OpcNodeCameraFunctionRelation>()
                .eq("camera_function_id", opcNodeCameraFunctionRelationDto.getCameraFunctionId()).eq("opc_node_id", opcNodeCameraFunctionRelationDto.getOpcNodeId()));
        if (!CollectionUtils.isEmpty(dbNodeCameraFunctionRelations)){
            log.info("===========================  节点：{} 与 {} 已绑定  ===========================",
                    opcNodeCameraFunctionRelationDto.getOpcNodeId(),opcNodeCameraFunctionRelationDto.getCameraFunctionId());
            return CommonResponse.ok("绑定关系已存在");
        }

        //新建绑定关系
        OpcNodeCameraFunctionRelation opcNodeCameraFunctionRelation = new OpcNodeCameraFunctionRelation();
        BeanUtils.copyProperties(opcNodeCameraFunctionRelationDto,opcNodeCameraFunctionRelation);
        opcNodeCameraFunctionRelation.setCreateUser(SecurityUtils.getLoginUserName());
        opcNodeCameraFunctionRelation.setUpdateTime(LocalDateTime.now());
        opcNodeCameraFunctionRelation.setUpdateUser(SecurityUtils.getLoginUserName());
        opcNodeCameraFunctionRelation.setUpdateTime(LocalDateTime.now());
        //保存绑定关系
        this.save(opcNodeCameraFunctionRelation);

        //准备放置redis缓存元素
//        OpcNodeInfo opcNodeInfo = iOpcNodeInfoService.getById(opcNodeCameraFunctionRelationDto.getCameraFunctionId());
//        String cacheKey = RedisConstants.OPC_NODE_IDENTIFIER + opcNodeInfo.getIdentifier();
//        CameraFunctionsPO cameraFunctionsPo = cameraFunctionsService.getById(opcNodeCameraFunctionRelationDto.getCameraFunctionId());
//        FunctionPO functionPo = functionService.getById(cameraFunctionsPo.getFunctionId());
//        redisTemplate.opsForList().rightPush(cacheKey,RedisConstants.SIGNAL+cameraFunctionsPo.getCameraId()+":"+functionPo.getType());

        return CommonResponse.ok("节点绑定成功");
    }
}
